<?php


namespace App\Remote\Feishu;


use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class FeishuMsgRemote extends FeishuRemote
{


    /**
     * 基础请求头构建
     *
     * @return array
     * @throws \Exception
     */
    public static function buildBasicHeaders(): array
    {
        return [
            'Content-Type'  => "application/json; charset=utf-8",
            'Authorization' => FeishuAuthRemote::getInstance()->getAccessToken()
        ]; // TODO: Change the autogenerated stub
    }


    /**
     * 发送富文本信息
     *
     * @param array $msgContent
     * @param array $userIds
     * @return mixed|void
     * @throws GuzzleException
     */
    public function batchSendPostMsg(array $msgContent, array $userIds)
    {
        return self::customRequest(
            "/open-apis/message/v4/batch_send",
            RequestAlias::METHOD_POST,
            [
                "msg_type" => "post",
                "content"  => $msgContent,
                "user_ids" => $userIds
            ]
        );
    }


    /**
     * 发送卡片信息
     *
     * @param string $msgContent
     * @param string $fsUserId
     * @return mixed|void
     * @throws GuzzleException
     */
    public function sendInteractiveMsg(string $msgContent, string $fsUserId)
    {
        return self::customRequest(
            "/open-apis/im/v1/messages?receive_id_type=user_id",
            RequestAlias::METHOD_POST,
            [
                "msg_type"   => "interactive",
                "content"    => str_replace(["\r", "\r\n", PHP_EOL, "  "], "", $msgContent),
                "receive_id" => $fsUserId
            ]
        );
    }


    /**
     * 批量发送卡片信息
     *
     * @param string $msgContent
     * @param array $fsUserIds
     * @return mixed|void
     * @throws GuzzleException
     */
    public function batchSendInteractiveMsg(string $msgContent, array $fsUserIds)
    {
        return self::customRequest(
            "/open-apis/message/v4/batch_send",
            RequestAlias::METHOD_POST,
            [
                "msg_type" => "interactive",
                "card"     => json_decode($msgContent, true),
                "user_ids" => $fsUserIds
            ]
        );
    }
}
